package com.kensure.controller;

import java.util.Collection;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import co.kensure.frame.ResultInfo;
import co.kensure.frame.ResultRowInfo;
import co.kensure.frame.ResultRowsInfo;
import co.kensure.http.RequestUtils;

import com.alibaba.fastjson.JSONObject;
import com.kensure.shike.changshang.model.ChangShang;
import com.kensure.shike.order.model.SKCar;
import com.kensure.shike.order.service.SKCarService;

/**
 * 购物车处理
 * 
 * @author fankaidi
 *
 */
@Controller
@RequestMapping(value = "car")
public class SKCarController {

	@Resource
	private SKCarService sKCarService;

	/**
	 * 前端提交购物车数据
	 */
	@ResponseBody
	@RequestMapping(value = "create.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo create(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		SKCar order = JSONObject.parseObject(json.toJSONString(), SKCar.class);
		sKCarService.add(order);
		return new ResultRowInfo(order);
	}

	/**
	 * 前端，根据用户获取购物车信息
	 */
	@ResponseBody
	@RequestMapping(value = "list.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo list(HttpServletRequest req, HttpServletResponse rep) {
		List<SKCar> list = sKCarService.qdlist();
		return new ResultRowsInfo(list);
	}
	
	/**
	 * 前端，根据用户获取购物车信息
	 */
	@ResponseBody
	@RequestMapping(value = "list1.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo list1(HttpServletRequest req, HttpServletResponse rep) {
		Collection<ChangShang> list = sKCarService.qdlist1();
		return new ResultRowsInfo(list);
	}

	/**
	 * 前端，删除购物车信息
	 */
	@ResponseBody
	@RequestMapping(value = "del.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo del(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		String ids = json.getString("ids");
		sKCarService.del(ids);
		return new ResultRowInfo();
	}
}
